var async = require("async");
var log=require("../../utils/log");
var proto=require("../../netbus/protocol");
var Cmd=require("../Cmd");
var utils=require("../../utils/utils");
var sql_gameSys=require("../../database/sql_gameSys");
var redis_gameSys=require("../../database/redis_gameSys");
//////////////////////////////////////////////////////
var server={
	name:"gameSys",	//服务名
	is_transfer:false, //验证不是转发模块

	//接收网关端发来的数据
	on_recv_client_cmd:function(session,stype,ctype,utag,proto_type,body){
		//body是一个object对象
		log.info(this.name+" 接收到的数据:",ctype,body);
		switch(ctype){
			case Cmd.Request.GameSys.USER_LOGIN:
				console.log("上线:",utag);
				user_login(session,utag,proto_type,body);
				break;
			case Cmd.Request.USER_DISCONNECT:
				console.log("离线:",utag);
				break;
		}
	},


	//与网关断开连接
	on_disconnect:function(session,stype){
		log.info(this.name+" 断开连接:",session.session_id);
		log.info("等待网关重连...");
	}
};

//游客登录
async function user_login(session,utag,proto_type,body){

	//验证数据合法性
	if (!body){
		session.send_data(Cmd.Stype.GameSys,Cmd.Request.GameSys.USER_LOGIN,utag,proto_type,{Respones:Cmd.Respones.INVALID_PARAMS});
	}

	var openId=body;

	var ret=await sql_gameSys.get_user_info_by_openId(openId);
	if (ret.err){
		session.send_data(Cmd.Stype.GameSys,Cmd.Request.GameSys.USER_LOGIN,utag,proto_type,{Respones:Cmd.Respones.SYSTEM_ERR});
		return;
	}

	//没有这个用户，注册一个
	if (ret.result.length<=0){
		await sql_gameSys.insert_user_info(openId);
		//得到id用于网关绑定
		ret=await sql_gameSys.get_user_info_by_openId(openId);
		ret=ret.result[0];
	}
	else{
		ret=ret.result[0];
	}

	//登陆成功
	session.send_data(Cmd.Stype.GameSys,Cmd.Request.GameSys.USER_LOGIN,utag,proto_type,{Respones:Cmd.Respones.OK,
		id:ret.id,gold:ret.gold,diamond:ret.diamond});


}

/*
//正式用户登录
async function user_login(session,utag,proto_type,body){

	//验证数据合法性
	if(!body){
		session.send_data(Cmd.Stype.Auth,Cmd.Request.Auth.USER_LOGIN,utag,proto_type,Cmd.Respones.INVALID_PARAMS);
		return;
	}

	//得到id用于网关绑定
	var ret=await mysql_center.get_user_info_by_name_pwd(body.phone,body.password);
	if (ret.err){
		session.send_data(Cmd.Stype.Auth,Cmd.Request.Auth.USER_LOGIN,utag,proto_type,{Respones:Cmd.Respones.SYSTEM_ERR});
		return;
	}

	//没有这个用户，说明用户名密码错误
	if (ret.result.length<=0){
		session.send_data(Cmd.Stype.Auth,Cmd.Request.Auth.USER_LOGIN,utag,proto_type,{Respones:Cmd.Respones.Auth.NAME_PWD_ERR});
	}
	else{
		ret=ret.result[0];
		//帐号被封
		if (ret.status!=0){
			session.send_data(Cmd.Stype.Auth,Cmd.Request.Auth.USER_LOGIN,utag,proto_type,{Respones:Cmd.Respones.ILLEGAL_ACCOUNT});
		}
		//是游客
		else if (ret.is_guest){
			session.send_data(Cmd.Stype.Auth,Cmd.Request.Auth.USER_LOGIN,utag,proto_type,{Respones:Cmd.Respones.INVALID_OPT});
		}
		else{
			//登陆成功
			session.send_data(Cmd.Stype.Auth,Cmd.Request.Auth.USER_LOGIN,utag,proto_type,{Respones:Cmd.Respones.OK,
				id:ret.id,nick:ret.nick,sex:ret.sex,face:ret.face,vip:ret.vip});


			//写入redis
			redis_center.set_user_info_inredis(ret.id,{
				nick:ret.nick,
				sex:ret.sex,
				face:ret.face,
				vip:ret.vip,
				is_guest:ret.is_guest,
			});
		}
	}
	
}
*/

module.exports=server;